﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Serialization;
using System.Globalization;
using System.IO;

namespace intelliEssay.Core.Platform
{
    /// <summary>
    /// Represents a component of the intelliEssay platform
    /// </summary>
    [XmlInclude(typeof(CheckerComponent))]
    public abstract class Component
    {
        internal Component()
        {
        }
        public string Type
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public string FilePath
        {
            get;
            set;
        }
        public bool IsLoaded
        {
            get;
            set;
        }
        public abstract void Load(string argument);
    }
    /// <summary>
    /// Represents a checker component
    /// </summary>
    [XmlInclude(typeof(Component))]
    public class CheckerComponent : Component
    {
        public string CheckerDescription
        {
            get;
            set;
        }
        [XmlIgnore]
        public IEssayChecker Checker
        {
            get;
            set;
        }
        public override void Load(string argument)
        {

            DirectoryInfo directoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
            string filePath = directoryInfo.FullName + "\\" + FilePath;
            if (!File.Exists(filePath))
            {
                IsLoaded = false;
                return;
            }
            Assembly assembly = Assembly.LoadFile(filePath);

            Type EssayChecker = assembly.GetExportedTypes().FirstOrDefault<Type>(type =>
                 {
                     return type.GetInterface("intelliEssay.Core.IEssayChecker") != null;
                 });
            Checker = Activator.CreateInstance(EssayChecker) as IEssayChecker;
            CheckerDescription = Checker.GetDescription(argument);
            Name = Checker.GetName(argument);
            IsLoaded = true;
        }
    }
}
